<template>
  <div>
    <h1>员工列表 欢迎{{ user.name }}</h1>
    <el-button
      type="primary"
      @click="handleInsert()">新增
    </el-button>
    <template>
      <el-table
        :data="userList.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
        style="width: 100%">
        <el-table-column
          label="ID"
          prop="id"
          width="100">
        </el-table-column>
        <el-table-column
          label="名字"
          prop="name"
          width="100">
        </el-table-column>
        <el-table-column
          label="性别"
          prop="sex"
          width="100">
        </el-table-column>
        <el-table-column
          prop="birthday"
          label="出生日期">
        </el-table-column>
        <el-table-column
          prop="registTime"
          label="注册日期">
        </el-table-column>
        <el-table-column
          align="right">
          <template slot="header" slot-scope="scope">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入名字搜索"/>
          </template>
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleLook(scope.row)">查看
            </el-button>
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <div class="block" :style="{marginTop:'20px',textAlign:'center'}">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5,10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum">
      </el-pagination>
    </div>

    <!--  弹框  -->
    <el-dialog title="员工信息" :visible.sync="dialogFormVisible">
      <el-form :model="userLook">
        <el-form-item label="ID" :label-width="formLabelWidth">
          <el-input v-model="userLook.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="userLook.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-input v-model="userLook.sex" disabled></el-input>
        </el-form-item>
        <el-form-item label="出生日期" :label-width="formLabelWidth">
          <el-input v-model="userLook.birthday" disabled></el-input>
        </el-form-item>
        <el-form-item label="注册日期" :label-width="formLabelWidth">
          <el-input v-model="userLook.registTime" disabled></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import Qs from 'qs'

export default {
  name: "Home",
  methods: {
    handleInsert() {
      this.$router.push(`/userAdd`);
    },
    handleLook(row) {
      this.userLook = row;
      this.dialogFormVisible = true;
    },
    handleEdit(row) {
      this.$router.push(`/userEdit/${row.id}`)
    },
    handleDelete(row) {
      let vm = this;
      // 发送删除数据的请求
      this.axios({
        url: `http://localhost:9090/vue/user/${row.id}`,
        method: 'GET'
      }).then(function (res) {
        // 判断是否删除成功
        if (res.data.code == 0) {
          vm.$message.success('删除成功');
          // 查询一下用户列表
          vm.getUserList(vm.currentPage, vm.pageSize);
        }
      }).catch(function (err) {
        vm.$message.error('删除失败');
      });
    },
    // 查询用户的方法
    getUserList(currentPage, pageSize) {
      // 发送axios请求
      let vm = this;
      this.axios({
        url: `http://localhost:9090/vue/users?pageIndex=${currentPage}&pageSize=${pageSize}`,
        method: 'GET'
      }).then(function (res) {
        console.log(res.data.data);
        vm.userList = res.data.data.list;
        vm.totalNum = res.data.data.total;
        vm.pageSize = res.data.data.pageSize;
        vm.currentPage = res.data.data.pageNum;
      });
    },
    handleSizeChange(val) {
      this.getUserList(this.currentPage, val);
    },
    handleCurrentChange(val) {
      this.getUserList(val, this.pageSize);
    }
  },
  data() {
    return {
      user: this.$store.getters.getUser, //从store中取出来数据
      userList: [],
      search: '',
      dialogFormVisible: false,
      formLabelWidth: '70px',
      userLook: {
        birthday: "",
        id: 0,
        name: "",
        password: "",
        registTime: "",
        sex: ""
      },
      totalNum: 0,
      currentPage: 1,
      pageSize: 10
    }

  },
  mounted() {
    // 页面挂载完成后，查询用户
    this.getUserList(this.currentPage, this.pageSize);
  }
}
</script>

<style scoped>

</style>
